﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Security.Cryptography;
using System.Text;

namespace Common {
	public class EncryptHelper {

		#region MD5
        		/// <summary>MD5函数(32bit or 16bit)</summary>
		/// <param name="str">原始字符串</param>
		/// <param name="iType">16 or 32</param>
		/// <returns>MD5结果</returns>
		public static string Md5(string str) {
            return Md5(str, 32);
        }
        /// <summary>MD5函数(32bit or 16bit)</summary>
		/// <param name="str">原始字符串</param>
		/// <param name="iType">16 or 32</param>
		/// <returns>MD5结果</returns>
		public static string Md5(string str, int iType) {
			byte[] b = Encoding.Default.GetBytes(str);
			b = new MD5CryptoServiceProvider().ComputeHash(b);
			string ret = "";

			for (int i = 0; i < b.Length; i++)
				ret += b[i].ToString("x").PadLeft(2, '0');

			if (iType == 16) {
				ret = ret.Substring(8, 16);
			}
			return ret;
		}
		#endregion
	}
}
